package org.example.socket;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Socket;
import java.util.Scanner;

public class SocketDemo {
    public static void main(String[] args) throws Exception {
        Socket localhost = new Socket("localhost", 3307);
        OutputStream out = localhost.getOutputStream();
        InputStream input = localhost.getInputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("输入信息：");
            String message = sc.nextLine();
            if (message.equals("quit")){
                break;
            }
            bw.write(message);
            bw.flush();
            System.out.println("等待服务端返回信息");
            byte[] bytes = new byte[1024];
            int read = input.read(bytes);
            if (read > 0) {
                System.out.println("接收到服务器信息：" + new String(bytes, 0, read));
            }
        }
        bw.close();
        out.close();
        input.close();
    }
}
